package com.tjetc.view.adminview;

import com.tjetc.proxy.CilentProxy;
import com.zhiyun.domain.Student;
import com.zhiyun.domain.Teacher;
import com.zhiyun.service.StudentService;
import com.zhiyun.service.TeacherService;
import com.zhiyun.vo.Result;

import java.util.List;
import java.util.Scanner;

public class Admin_User {
    StudentService studentService= CilentProxy.getProxyInstance(StudentService.class);
    TeacherService teacherService=CilentProxy.getProxyInstance(TeacherService.class);
    Scanner scanner=new Scanner(System.in);
    public void start() {
        while (true){
            System.out.println("~~~~~~~~~用户管理~~~~~~~~~");
            System.out.println("1. 学生管理");
            System.out.println("2. 教师管理");
            System.out.println("0. 退出");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    studentManage();
                    break;
                case 2:
                    teacherManage();
                    break;
                case 0:

                    return;
                default:
                    break;
            }
        }
    }

    private void teacherManage() {
        while (true){
            Result result = teacherService.AllTeacher();
            if (result.getCode()==200){
                List<Teacher> obj = (List<Teacher>) result.getObj();
                for (Teacher teacher : obj) {
                    System.out.println("老师id:"+teacher.getId()+"      老师姓名:"+teacher.getTruename()+"     老师昵称:"+teacher.getNickname()+
                            "      老师电话:"+teacher.getPhone()+"     老师性别:"+teacher.getSex()+"      老师邮箱:"+teacher.getEmail());
                }
                System.out.println("请输入想要修改的老师id(   -1  退出   )");
                int i = scanner.nextInt();
                if (i==-1){
                    return;
                }else {
                    boolean a=false;
                    for (Teacher teacher : obj) {
                        if (i==teacher.getId()){
                            a=true;
                            break;
                        }
                    }
                    if (a){
                        Result result1 = teacherService.myInfo(i);
                        if (result1.getCode()==200){
                            Teacher obj1 = (Teacher) result1.getObj();
                            System.out.println("请输入修改后的公司");
                            String company = scanner.next();
                            System.out.println("请输入修改后的工号");
                            Integer cid = scanner.nextInt();
                            obj1.setCompany(company);
                            obj1.setCid(cid);
                            Result result2 = teacherService.updateTeacher(obj1);
                            if (result2.getCode()==200){
                                System.out.println(result2.getMsg());
                            }else {
                                System.out.println(result2.getMsg());
                            }
                        }else {
                            System.out.println(result1.getMsg());
                        }
                    }else {
                        System.out.println("你想要修改的老师id不存在");
                    }
                }
            }else {
                System.out.println(result.getMsg());
            }
            break;
        }
    }

    private void studentManage() {
        while (true){
            Result result = studentService.AllStudent();
            if (result.getCode()==200){
                List<Student> obj1 = (List<Student>) result.getObj();
                for (Student student : obj1) {
                    System.out.println("学生id:"+student.getId()+"      学生姓名:"+student.getTruename()+"     学生昵称:"+student.getNickname()+
                            "      学生电话:"+student.getPhone()+"     学生性别:"+student.getSex()+"      学生邮箱:"+student.getEmail());
                }
                System.out.println("请输入想要修改的学生id(   -1  退出   )");
                int i = scanner.nextInt();
                if (i==-1){
                    return;
                }
                Result result1 = studentService.myInfo(i);
                if (result1.getCode()==200){
                    Student obj = (Student) result1.getObj();
                    System.out.println("请输入修改后的学校");
                    String school = scanner.next();
                    System.out.println("请输入修改后的学号");
                    Integer sid = scanner.nextInt();
                    obj.setSchool(school);
                    obj.setSid(sid);
                    Result result2 = studentService.updateStudent(obj);
                    if (result2.getCode()==200){
                        System.out.println(result2.getMsg());
                    }else {
                        System.out.println(result2.getMsg());
                    }
                }else {
                    System.out.println(result1.getMsg());
                }
            }else {
                System.out.println(result.getMsg());
            }
            break;
        }
    }
}
